#!/bin/sh

cilly=$1/cilly.byte.exe
CIL_MACHINE=`$1/machdep-ml32.exe --env`
export CIL_MACHINE

mkdir -p temp

for test in *.c; do 
    base=`basename $test .c`
    echo -n "$base... "
    gcc -o temp/$base.gcc $test
    temp/$base.gcc >temp/$base.gcc.output
    gcc -m32 -E $test -o temp/$base.i
    $cilly --dollvm --envmachine temp/$base.i >temp/$base.ll
    if llvm-as -f temp/$base.ll >/dev/null 2>/dev/null && 
       llc -march=x86 -f temp/$base.bc >/dev/null 2>/dev/null &&
       gcc -m32 -o temp/$base.llvm temp/$base.s >/dev/null 2>/dev/null &&
       temp/$base.llvm >temp/$base.llvm.output; then
      if cmp temp/$base.gcc.output temp/$base.llvm.output; then
        echo PASS
      else
        failed="$failed $base"
        echo RUN FAILED
        diff temp/$base.gcc.output temp/$base.llvm.output
      fi
    else
      failed="$failed $base"
      echo COMPILE FAILED
    fi
done

echo
if [ -z "$failed" ]; then
  echo ALL TESTS PASSED
else
  echo $failed FAILED
fi
